package com.manor.paradise.activity

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ConvertUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.core.BasePopupView
import com.manor.common.activity.AbsActivity
import com.manor.common.em.AuthType
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.http.ElvesHttpUtil
import com.mob.adsdk.WebFragment
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_fairy_park_new.*

/**
 * 淘米乐园首页
 */
@Route(path = RouteUtil.PC_PAIRY_FAIRY)
class FairyParkActivity : AbsActivity(), View.OnClickListener {
    private val maxRetry = 3
    private var curRetry = 0
    private var mXpopup: BasePopupView? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_fairy_park_new
    }


    override fun main() {
        super.main()
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init()
        initView()
    }

    override fun onResume() {
        super.onResume()
        getMode()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    private fun initView() {
        im_back?.setOnClickListener(this)
        flWorld?.setOnClickListener(this)
        ivIntroduction?.setOnClickListener(this)
        ivCash?.setOnClickListener(this)
        ivFarm?.setOnClickListener(this)
        ivParadise?.setOnClickListener(this)
        ivHatching?.setOnClickListener(this)
        ClickUtils.applyPressedViewScale(im_back, flWorld, ivIntroduction, ivCash, ivFarm, ivParadise, ivHatching)

    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick())
            when (v?.id) {
                R.id.im_back -> {
                    ActivityUtils.finishActivity(this)
                }
                R.id.flWorld -> {
                    ActivityUtils.startActivity(MyWorldActivity::class.java)
                }
                R.id.ivIntroduction -> {
                    showPopup(ivIntroduction)
                }
                R.id.ivCash -> {
                    // 现金任务，跳web
                    WebTaskActivity.forward(false)
                }
                R.id.ivFarm -> {
                    ActivityUtils.startActivity(MyEmploymentActivity::class.java)
                }
                R.id.ivParadise -> {
                    //  淘米庄园。跳web
                    WebTaskActivity.forward(true)
                }
                R.id.ivHatching -> {
                    ActivityUtils.startActivity(MineActivity::class.java)
                }
            }
    }


    private fun showPopup(m_view: View) {
        val contentView = LayoutInflater.from(this).inflate(R.layout.dialog_sm, null)
        val mPopWindow = PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true)
        mPopWindow.contentView = contentView
        mPopWindow.animationStyle = R.style.bottomToTopAnim

        val tvMsg = contentView.findViewById<ImageView>(R.id.ivImg)

        if (mPopWindow.isShowing) {
            mPopWindow.dismiss()
        } else {
            mPopWindow.showAtLocation(m_view, Gravity.BOTTOM,
                    0, ConvertUtils.dp2px(120f))
        }
        contentView.setOnClickListener {
            mPopWindow.dismiss()
        }
        tvMsg.setOnClickListener {
            mPopWindow.dismiss()
        }

    }

    var jlLyEnable = ""
    private var isJinru = false
    private fun getMode() {
        ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    if (curRetry >= maxRetry) {
                        finish()
                    } else {
                        curRetry++
                        getMode()
                        ToastUtil.show(msg)
                        return
                    }
                }
                dismissWaitingDialog()
                var paradiseInfoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                jlLyEnable = paradiseInfoBean.jlLyEnable


                SpUtil.getInstance().setBooleanValue(SpUtil.FRIRYLAND_BGETSPIRIT, paradiseInfoBean.bgetSpirit)

                if (!isActDestroy(this@FairyParkActivity))
                    Glide.with(this@FairyParkActivity).load(paradiseInfoBean.avatar)
                            .apply(RequestOptions().transform(CenterCrop(), CircleCrop()))
                            .into(im_av)



                tvConfirm.setGone(true)
                tvDismiss.text = "取消"

                if (paradiseInfoBean.isAuth == 1) {
                    isJinru = true
                    flAuth.visibility = View.GONE
                } else {
                    flAuth.visibility = View.VISIBLE
                    flAuth.setOnClickListener {}


                    im_close.setOnClickListener { finish() }
                    tv_gz.setOnClickListener { RouteUtil.forwardPath(RouteUtil.PC_WEB, StaIntentUtil.VIP_EXPERIENCE) }
                    tv_rz.setOnClickListener {
                        if (ClickUtil.canClick(1500))
                            setCamera()
                    }
                    tvConfirm.setOnClickListener { RouteUtil.forwardPath(RouteUtil.PC_WEB, StaIntentUtil.VIP_EXPERIENCE) }
                    tvDismiss.setOnClickListener {
                        ActivityUtils.finishActivity(this@FairyParkActivity)
                    }

                }
            }

            override fun onError() {
                super.onError()
                if (curRetry >= maxRetry) {
                    ToastUtil.show("暂时无法进入淘米庄园，请稍后重试")
                    finish()
                } else {
                    curRetry++
                    getMode()
                }
            }
        })
    }


    private fun setCamera() {
        AndPermission.with(this)
                .runtime()
                .permission(Permission.CAMERA)//相机
                .onGranted {
                    RouteUtil.forwardFaceExp(AuthType.TYPE_AUTH)
                    mXpopup?.dismiss()
                }
                .onDenied { data ->
                    Toast.makeText(this, resources.getString(R.string.permissions_camera),
                            Toast.LENGTH_SHORT).show()
                    if (AndPermission.hasAlwaysDeniedPermission(this, data)) {
                        showSettingDialog(this,
                                resources.getString(R.string.permissions_camera), 0)
                    } else {
                        setCamera()
                    }
                }
                .start()
    }


}
